package bigo.data;

import java.util.ArrayList;

/**
 *
 * @author songliu
 */
public class SmartMatrix {

    private Matrix m;
    private boolean isTransposed;
    private int rowOffset;
    private int columnOffset;

    public SmartMatrix() {
    }

    public SmartMatrix(Matrix m, boolean isTransposed) {
        this.m = m;
        this.isTransposed = isTransposed;
        if (!isTransposed) {
            rowOffset = m.getRowVectorAt(m.getColumnOffset()).getOffset();
            columnOffset = m.getColumnOffset();
        } else {
            columnOffset = m.getRowVectorAt(m.getColumnOffset()).getOffset();
            rowOffset = m.getColumnOffset();
        }
    }

    public ArrayList<ArrayVector> multiply(SmartMatrix matrix, boolean rowBased) {
        ArrayList<ArrayVector> ret = new ArrayList<ArrayVector>();

        if (rowBased) {
            int start = getColumnOffset();
            for (int i = start; i < start + getNumRows(); i++) {
                Vector lhs = getRowAt(i);
                ret.add(lhs.multiplyWith(matrix));
            }
        } else {
            int start = matrix.getRowOffset();
            for (int i = start; i < start + matrix.getNumColumns(); i++) {
                Vector lhs = matrix.getColumnAt(i);
                ret.add(lhs.multiplyWithT(this));
            }
        }
        return ret;
    }

    public float get(int i, int j) {
        if (isTransposed) {
            return m.get(j, i);
        } else {
            return m.get(i, j);
        }
    }

    public Matrix getM() {
        return m;
    }

    public int getRowOffset() {
        return rowOffset;
    }

    public int getColumnOffset() {
        return columnOffset;
    }

    public int getNumRows() {
        if(isTransposed){
            return m.getNumColumns();
        }else{
            return m.getNumRows();
        }
    }

    public int getNumColumns(){
        if(isTransposed){
            return m.getNumRows();
        }else{
            return m.getNumColumns();
        }
    }

    public Vector getRowAt(int rowIndex) {
        if (isTransposed) {
            return m.getColumnVectorAt(rowIndex);
        } else {
            return m.getRowVectorAt(rowIndex);
        }

    }

    public Vector getColumnAt(int colIndex) {
        if (isTransposed) {
            return m.getRowVectorAt(colIndex);
        } else {
            return m.getColumnVectorAt(colIndex);
        }

    }
}
